﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;
using Rafy;
using Rafy.ComponentModel;
using Rafy.Data;
using Rafy.Domain;
using Rafy.Domain.ORM;
using Rafy.Domain.ORM.Query;
using Rafy.Domain.Validation;
using Rafy.ManagedProperty;
using Rafy.MetaModel;
using Rafy.MetaModel.Attributes;
using static Rafy.Domain.ORM.Query.FactoryMethods;

namespace Rafy.Book
{
    /// <summary>
    /// 图书目录
    /// </summary>
    [RootEntity]
    public partial class Base_CatalogBook : BookEntity
    {
        #region 引用属性

        #endregion

        #region 组合子属性
        //public static readonly ListProperty<Base_ButtonList> Base_ButtonListProperty = P<Base_CatalogBook>.RegisterList(e => e.Base_ButtonList);
        //public Base_ButtonList Base_ButtonList
        //{
        //    get { return this.GetLazyList(Base_ButtonListProperty); }
        //}
        #endregion

        #region 一般属性
        public static readonly Property<string> F_NameProperty = P<Base_CatalogBook>.Register(e => e.F_Name);
        /// <summary>
        /// 目录名称
        /// </summary>
        public string F_Name
        {
            get { return this.GetProperty(F_NameProperty); }
            set { this.SetProperty(F_NameProperty, value); }
        }

        public static readonly Property<int> F_ClsProperty = P<Base_CatalogBook>.Register(e => e.F_Cls);
        /// <summary>
        /// 分类20=单元目录，10=章节目录
        /// </summary>
        public int F_Cls
        {
            get { return this.GetProperty(F_ClsProperty); }
            set { this.SetProperty(F_ClsProperty, value); }
        }

        public static readonly Property<int> F_BooksIdProperty = P<Base_CatalogBook>.Register(e => e.F_BooksId);
        /// <summary>
        /// 所属书籍
        /// </summary>
        public int F_BooksId
        {
            get { return this.GetProperty(F_BooksIdProperty); }
            set { this.SetProperty(F_BooksIdProperty, value); }
        }

        public static readonly Property<int> F_CountProperty = P<Base_CatalogBook>.Register(e => e.F_Count);
        /// <summary>
        /// 当前已选择数量
        /// </summary>
        public int F_Count
        {
            get { return this.GetProperty(F_CountProperty); }
            set { this.SetProperty(F_CountProperty, value); }
        }

        #endregion

        #region 只读属性
        public Base_ButtonList Buttons { get; set; }
        #endregion
    }

    /// <summary>
    /// 图书目录 列表类。
    /// </summary>
    public partial class Base_CatalogBookList : BookEntityList<Base_CatalogBook> { }

    /// <summary>
    /// 图书目录 仓库类。
    /// 负责 图书目录 类的查询、保存。
    /// </summary>
    public partial class Base_CatalogBookRepository : BookEntityRepository
    {
        /// <summary>
        /// 单例模式，外界不可以直接构造本对象。
        /// </summary>
        protected Base_CatalogBookRepository() { }

        [RepositoryQuery]
        public virtual Base_CatalogBookList GetsBy(int? bookId)
        {
            var q = this.CreateLinqQuery();
            q = q.Where(e => e.F_BooksId == bookId);
            return (Base_CatalogBookList)this.QueryData(q);
        }
    }

    /// <summary>
    /// 图书目录 配置类。
    /// 负责 图书目录 类的实体元数据的配置。
    /// </summary>
    internal class Base_CatalogBookConfig : BookEntityConfig<Base_CatalogBook>
    {
        /// <summary>
        /// 配置实体的元数据
        /// </summary>
        protected override void ConfigMeta()
        {
            //配置实体的所有属性都映射到数据表中。
            Meta.MapTable().MapAllProperties();
        }
    }
}